package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.user.UpdateRoleVo;
import com.wanxi.education.model.vo.user.AUserReturnVo;
import com.wanxi.education.model.vo.user.AUserVo;
import com.wanxi.education.model.vo.user.FindByIdAUserVo;
import com.wanxi.education.model.vo.user.UpdateAUserVo;
import com.wanxi.education.service.AUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: YanZhengYi
 * @Description:作用于管理员管理界面
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Api(tags = "A端用户管理")
@RestController
@RequestMapping("/back/aUser")
public class AUserController {
    @Autowired
    private AUserService aUserService;

    @GetMapping("/findPage")
    @ApiOperation("查询所有的用户数据")
    public CommonResult findPage(AUserVo aUserVo){
        PageInfo<AUserReturnVo> aUserReturnVo = aUserService.aUserFindPage(aUserVo);
        return CommonResult.success(aUserReturnVo);
    }

    @PostMapping("/update")
    @ApiOperation("更新数据")
    public CommonResult update(@RequestBody UpdateAUserVo updateAUserVo){
        Integer result = aUserService.aUserUpdate(updateAUserVo);
        if (result==null)return CommonResult.failure("更新失败,用户名或身份证号重复");
        return CommonResult.success("success");
    }

    @PostMapping("/delete")
    @ApiOperation("删除一行")
    public CommonResult delete(Long id){
        Integer result =  aUserService.AdminDelete(id);
        if (result==null) return CommonResult.failure("删除失败");
        return CommonResult.success("success");
    }

    @PostMapping("/insert")
    @ApiOperation("添加数据")
    public CommonResult insert(@RequestBody UpdateAUserVo updateAUserVo){
        Integer result = aUserService.aUserInsert(updateAUserVo);
        if (result==null) return CommonResult.failure("添加失败,电话重复");
        return CommonResult.success("success");
    }

    @GetMapping("/findById")
    @ApiOperation("通过Id进行回显数据")
    public CommonResult<FindByIdAUserVo> findById(Long id){
        FindByIdAUserVo findByIdAUserVo = aUserService.findById(id);
        return CommonResult.success(findByIdAUserVo);
    }


    @PostMapping("/updateRole")
    @ApiOperation("分配角色")
    public CommonResult updateRole(@RequestBody UpdateRoleVo updateRoleVo){
        Integer result = aUserService.updateRole(updateRoleVo);
        if (result==null)return CommonResult.failure("分配失败，请稍后再试");
        return CommonResult.success("success");
    }
}
